<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Dean <zxxjjforever@163.com>
// +----------------------------------------------------------------------
// | UploadViewUaf Plugin Author: Foxjun<jun777@qq.com>
// +----------------------------------------------------------------------
//
namespace plugins\qiniu_view_uaf\controller; 
use cmf\controller\PluginBaseController;
use think\Db;


class IndexController extends PluginBaseController
{

    function index()
    {   

        $qconfig = array();
        //七牛插件检查
        if(!self::checkQiniuState()){
            echo '请先安装系统自带七牛云存储插件并正确设置！';
            return ;
        }else{

            $qconfig = DB::name('plugin')->where('name','Qiniu')->value('config');
            $qconfig = json_decode($qconfig, true);

            if(empty($qconfig['accessKey']) || empty($qconfig['secretKey']) || empty($qconfig['domain']) || empty($qconfig['bucket']) ){
                echo '请正确设置七牛云参数';
                return ;
            }            
        }



        $where = array();

        $config = $this->getPlugin()->getConfig();

        //获取文件类型
        $filetype = request()->param('filetype');
        
        

            //获取上传设置
            $sconfig = cmf_get_upload_setting();
            //根据当前文件类型获取对应扩展名
            $type = $sconfig['file_types'][$filetype]['extensions'];


            $where['suffix'] = array('in',$type);

        

        $where['user_id'] = cmf_get_current_admin_id();
        $result = Db::name('asset')
            ->where($where)
            ->where('file_md5','EQ','')
            ->order('create_time', 'DESC')
            ->paginate(12);

        $items = Self::makeData($result->items(),$qconfig);
        

        $imgtype = trim($config['imgtype']);
        if(!empty($imgtype)){
            $imgtype = explode(",",$imgtype);
        }

        $this->assign('assets', $items);
        $this->assign('page', $result->render());
        $this->assign('imgtype',$imgtype);
        $this->assign('config',$config);
        $this->assign('qconfig',$qconfig);
        $this->assign('filetype',$filetype);

        return $this->fetch("/index");
    }

    public function makeData($items,$qconfig)
    {   
        $domain = trim($qconfig['domain']);
        if(!preg_match('/^http(s)?:\\/\\/.+/',$domain)){
            $domain = $qconfig['protocol'].'://'.$domain.'/';
        }

        foreach ($items as $key => $value) {
            $items[$key]['url'] = $domain.$value['file_path'];
        }
        return $items;

    }   

    /**
     * [checkQiniuState 检查七牛插件是否注册]
     * @return [type] [description]
     */
    public function checkQiniuState()
    {
        $r = array();
        $r = DB::name('hook_plugin')->where('plugin','Qiniu')->find();
        $r = empty($r)?false:true;
        return $r;
    }


}
